package com.zsh.weather.ln.home

import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.zsh.weather.ln.R
import com.zsh.weather.ln.base.BaseActivity
import com.zsh.weather.ln.home.ui.care.CareFragment
import com.zsh.weather.ln.home.ui.home.HomeFragment
import com.zsh.weather.ln.home.ui.search.SearchFragment
import kotlinx.android.synthetic.main.activity_home.*

class HomeActivity : BaseActivity() {
    override fun getLayoutId(): Int {
        return R.layout.activity_home
    }

    override fun initView() {
        nav_view_pager.adapter = ViewPager2Adapter(mActivity as HomeActivity)
        nav_view.setOnNavigationItemSelectedListener {
            when(it.itemId){
                R.id.navigation_home -> nav_view_pager.currentItem = 0
                R.id.navigation_serach -> nav_view_pager.currentItem = 1
                R.id.navigation_care -> nav_view_pager.currentItem = 2
            }
            return@setOnNavigationItemSelectedListener true
        }
        nav_view_pager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                nav_view.menu.getItem(position).setChecked(true)
            }
        })
    }

    /*
    * viewpager2 adapter
    * */
    class ViewPager2Adapter(val mActivity: HomeActivity) :
        FragmentStateAdapter(mActivity) {
        override fun getItemCount(): Int {
            return 3
        }

        override fun createFragment(position: Int): Fragment {
            when (position) {
                0 -> return HomeFragment()
                1 -> return SearchFragment()
                2 -> return CareFragment()
                else -> return Fragment()
            }
        }
    }
}